package com.example.demo;

import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * Created by sunmo on 2016/12/4.
 */
public class FluentFlingAnim implements ViewPager.PageTransformer {
    private static final float MIN_SCALE = 0.75f;
//    private static final float MIN_ALPHA = 0.75f;
    private static final float MIN_ALPHA = 1f;
    private static final float MAX_ROTATE = 60;

    @Override
    public void transformPage(View page, float position) {
        int pageWidth = page.getWidth();
        int pageHeight = page.getHeight();
        float rotateFactor = MAX_ROTATE * Math.abs(position - (int) position);//旋转角度[-MAX,MAX]

        if (position < -1) {//左边隐藏的动画初始化设置
            page.setRotationY(MAX_ROTATE);//setRotate系列方法直接设置角度制角度，千万把有些时候要与用弧度值的时候区分开来
            page.setAlpha(MIN_ALPHA);
            page.setScaleX(MIN_SCALE);
            page.setScaleY(MIN_SCALE);
        } else if (position < 1) {
            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            float alphaFactor = Math.max(MIN_ALPHA, 1 - Math.abs(position));
            float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            float horzMargin = pageWidth * (1 - scaleFactor) / 2;
            if (position < 0) {//0-->-1   //设置中间pager动画
                page.setTranslationX(horzMargin - vertMargin / 2);
                page.setRotationY(rotateFactor);
                page.setAlpha(alphaFactor);

            } else {//1-->0 //设置右边图片右边pager的动画
                page.setTranslationX(-horzMargin + vertMargin / 2);
                page.setRotationY(-rotateFactor);
                page.setAlpha(alphaFactor);
            }
            page.setScaleX(scaleFactor);
            page.setScaleY(scaleFactor);
        } else {//左边隐藏的动画初始化设置
            page.setRotationY(-MAX_ROTATE);
            page.setAlpha(MIN_ALPHA);
            page.setScaleX(MIN_SCALE);
            page.setScaleY(MIN_SCALE);
        }
    }
}
